package com.lm.liaoming.base;

/**
 * Created by Administrator on 2019/7/1.
 */

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import com.tumblr.remember.Remember;
import com.lm.liaoming.R;
import com.lm.liaoming.constant.ConstantValues;
import com.lm.liaoming.tool.Toaster;

import java.io.ByteArrayOutputStream;

public abstract class BaseDialogFragment extends DialogFragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(setLayout(), container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onViewCreat(view);
    }

    /*是否底部展示*/
    public abstract boolean setBottom();

    public abstract void onViewCreat(View view);

    public abstract int setLayout();

    public boolean isLogin() {
        boolean b = Remember.getBoolean(ConstantValues.LOGIN_STATUS, false);
        return b;
    }

    public String getToken() {
        String string = Remember.getString(ConstantValues.TOKEN_VALUE, "");
        return string;
    }

    public void showToast(String msg) {
        Toaster.showToast(getContext(), msg);
    }


    /*图片地址转换二进制流*/
    public byte[] imageTransformation(String path) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        return stream.toByteArray();
    }

    @Override
    public void onStart() {
        super.onStart();
        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        if (setBottom()) {
            params.windowAnimations = R.style.dialogAnim;
            params.gravity = Gravity.BOTTOM; // 紧贴底部
        }
        params.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        getDialog().getWindow().setAttributes(params);

    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (setBottom()) {
            return new Dialog(getActivity(), R.style.BottomDialog);
        } else {
            return super.onCreateDialog(savedInstanceState);
        }
    }
}
